<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="140px"
      v-loading="formLoading"
    >
      <el-form-item label="支付订单日期" prop="orgReqDate">
        <el-date-picker
          v-model="formData.orgReqDate"
          type="date"
          value-format="x"
          placeholder="选择支付订单日期"
          disabled
        />
      </el-form-item>
      <el-form-item label="支付订单流水号" prop="orgHfSeqId">
        <el-input v-model="formData.orgHfSeqId" placeholder="请输入支付订单流水号" disabled />
      </el-form-item>
      <el-form-item label="收款商户号" prop="huifuId">
        <el-input v-model="formData.huifuId" placeholder="请输入收款商户号"  disabled />
      </el-form-item>
      <el-form-item label="收款商户名称" prop="merName">
        <el-input v-model="formData.merName" placeholder="请输入收款商户名称" disabled />
      </el-form-item>
      <el-form-item label="交易总金额" prop="transAmt">
        ￥{{ fenToYuan(formData.transAmt) }} 
      </el-form-item>
      <el-form-item label="分账比例" prop="rebateRate">
        {{ percent(formData.rebateRate) }}
      </el-form-item>
      <el-form-item label="分账金额" prop="divAmt">
        ￥{{ fenToYuan(formData.divAmt) }} 
      </el-form-item>
      <el-form-item label="分账接收方编号" prop="divHuifuId">
        <el-input v-model="formData.divHuifuId" placeholder="请输入分账接收方编号" disabled />
      </el-form-item>
      <el-form-item label="请求日期" prop="reqDate">
        <el-date-picker
          v-model="formData.reqDate"
          type="date"
          value-format="x"
          placeholder="选择请求日期"
          disabled
        />
      </el-form-item>
      <el-form-item label="请求流水号" prop="reqSeqId">
        <el-input v-model="formData.reqSeqId" placeholder="请输入请求流水号" disabled  />
      </el-form-item>
      <el-form-item label="请求发起结果" prop="transState">
        <el-input v-model="formData.transState" placeholder="请输入请求发起结果" disabled  />
      </el-form-item>
      <el-form-item label="返回状态码" prop="respCode">
        <el-input v-model="formData.respCode" placeholder="请输入返回状态码" disabled  />
      </el-form-item>
      <el-form-item label="返回信息描述" prop="respDesc">
        <el-input v-model="formData.respDesc" placeholder="请输入返回信息描述" disabled />
      </el-form-item>
      <el-form-item label="重试次数" prop="reqCount">
        <el-input v-model="formData.reqCount" placeholder="请输入重试次数" disabled  />
      </el-form-item>
   
      <el-descriptions :column="1" label-class-name="desc-label" direction="vertical" border>
        <el-descriptions-item label="请求参数">
        <el-text style="white-space: pre-wrap; word-break: break-word">
          {{ formData.reqParam }}
        </el-text>
      </el-descriptions-item>
      <el-descriptions-item label="请求结果">
        <el-text style="white-space: pre-wrap; word-break: break-word">
          {{ formData.reqResponse }}
        </el-text>
      </el-descriptions-item>
    </el-descriptions>
  </el-form>
     
    <template #footer>
      <!-- <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> -->
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { fenToYuan,percent } from '@/utils'
import { DelayTransOrderApi, DelayTransOrderVO } from '@/api/pay/delaytransorder'

/** 汇付延迟结算支付订单分账记录	 表单 */
defineOptions({ name: 'DelayTransOrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  orgReqDate: undefined,
  orgHfSeqId: undefined,
  huifuId: undefined,
  merName: undefined,
  transAmt: 0,
  rebateRate: 0,
  divAmt: 0,
  divHuifuId: undefined,
  reqDate: undefined,
  reqSeqId: undefined,
  transState: undefined,
  respCode: undefined,
  respDesc: undefined,
  reqParam: undefined,
  reqResponse: undefined,
  reqCount: undefined,
})
const formRules = reactive({
  orgReqDate: [{ required: true, message: '支付订单日期不能为空', trigger: 'blur' }],
  orgHfSeqId: [{ required: true, message: '支付订单流水号不能为空', trigger: 'blur' }],
  huifuId: [{ required: true, message: '收款商户号不能为空', trigger: 'blur' }],
  merName: [{ required: true, message: '收款商户名称不能为空', trigger: 'blur' }],
  transAmt: [{ required: true, message: '交易总金额不能为空', trigger: 'blur' }],
  rebateRate: [{ required: true, message: '分账比例不能为空', trigger: 'blur' }],
  divAmt: [{ required: true, message: '分账金额不能为空', trigger: 'blur' }],
  divHuifuId: [{ required: true, message: '分账接收方编号不能为空', trigger: 'blur' }],
  reqDate: [{ required: true, message: '请求日期不能为空', trigger: 'blur' }],
  reqSeqId: [{ required: true, message: '请求流水号不能为空', trigger: 'blur' }],
  transState: [{ required: true, message: '请求发起结果不能为空', trigger: 'blur' }],
  reqParam: [{ required: true, message: '请求参数不能为空', trigger: 'blur' }],
  reqResponse: [{ required: true, message: '请求结果不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DelayTransOrderApi.getDelayTransOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DelayTransOrderVO
    if (formType.value === 'create') {
      await DelayTransOrderApi.createDelayTransOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await DelayTransOrderApi.updateDelayTransOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    orgReqDate: undefined,
    orgHfSeqId: undefined,
    huifuId: undefined,
    merName: undefined,
    transAmt: undefined,
    rebateRate: undefined,
    divAmt: undefined,
    divHuifuId: undefined,
    reqDate: undefined,
    reqSeqId: undefined,
    transState: undefined,
    respCode: undefined,
    respDesc: undefined,
    reqParam: undefined,
    reqResponse: undefined,
    reqCount: undefined,
  }
  formRef.value?.resetFields()
}

</script>